<template>
  <div id="login" class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/css/img.webp" alt="" />
      </div>
      <!-- 登陆区域 -->
      <el-form
        ref="loginFormRef"
        label-width="0px"
        class="login_form"
        :model="loginForm"
        :rules="loginRules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <!-- 登录密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-s-goods"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item class="btns" prop="btns">
          <el-button type="primary" @click="login">登陆</el-button>
          <el-button type="info" @click="resetLogin">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  el: 'login',
  data() {
    return {
      // 表单的数据绑定对象
      loginForm: {
        username: "admin",
        password: "123456",
      },
      // 表单的登陆规则
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          { min:6, max: 12, message: "长度在6 到 12 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //重置登陆表单
    resetLogin() {
      // console.log(this);
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate( async valid => {
        if(!valid) return;
        //请求状态，并解构赋值给result
        const {data: res} = await this.$http.post('login', this.loginForm);
        //console.log(this.$http.post('login', this.loginForm))
        //判断登陆情况
        if(res.meta.status !== 200) 
        //this.$message.XXX()是elementUI 自带的弹窗
        return this.$message.error('登录失败');
        this.$message.success('登录成功')

        //1、登录成功的token，保存到客户端的sessionStorage中
          //其他API接口必须在登陆后才能使用，并且token只在当前网站打开期间生效
        window.sessionStorage.setItem("token", res.data.token);
        //2、编程式导航跳转 /home
        this.$router.push('/home');
      });
    }
  },
};

</script>

<style lang="less" scoped>
.login_container {
  box-sizing: border-box;
  height: 100%;
  background-color: #2b4b6b;
}

.login_box {
  box-sizing: border-box;
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  margin: 0 auto;
  position: relative;
  top: -65px;
  box-sizing: border-box;
  height: 130px;
  width: 130px;
  border: 1px solid black;
  border-radius: 50%;
  box-sizing: 0 0 10 #ddd;
  background-color: #fff;
}

img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.login_form {
  width: 100%;
  position: absolute;
  bottom: 0px;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>